package com.cn.wanxi.service;

import com.cn.wanxi.dao.NewsDao;
import com.cn.wanxi.model.NewsModel;
import com.cn.wanxi.util.IO;
import com.cn.wanxi.util.PAHT;
import com.cn.wanxi.util.Tool;

import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;

/**
 * 新闻控制层
 */
public class NewsService {
    private String split = PAHT.SPLIT.getPath();
    /**
     * /得到home新闻
     * @return
     */
    public ArrayList<NewsModel> getNewsModelListForNewsCenter() {
        List<String> newsImgList = new ArrayList();
        List<String> newsTextTitleList = new ArrayList();
        List<String> newsTextList = new ArrayList();

        NewsDao navDao = new NewsDao();
        int length = navDao.getNewsImgsList().size();
        for (int i = 0; i <length; i++) {
            newsImgList.add(navDao.getNewsImgsList().get(i));
            newsTextTitleList.add(navDao.getNewsTextTitlesList().get(i));
            newsTextList.add(navDao.getNewsTextsList().get(i));
        }
        String Img = Tool.replaceText(newsImgList.toString());
        String TextTitle = Tool.replaceText(newsTextTitleList.toString());
        String Text = Tool.replaceText(newsTextList.toString());

        String addStr = Img+split+TextTitle+split+Text;
        //写
        IO.writeNotepad(addStr, PAHT.NEWSSERVICE_FILE_NAME.getPath());
        //读
        String[] str = IO.readNotepad().split(split);//aaaaaa@@@@bbbbbbb

        List<String> List=new ArrayList(Arrays.asList(str));//集合1{a,a,a,a,a,a,a}和集合2{b,b,b,b,b,b,b,b}
        ArrayList<NewsModel> newsModelList = new ArrayList<>();
        for (int i = 0; i <length; i++) {
            NewsModel newsModel = new NewsModel();
            newsModel.setNewsImg(List.get(0).split(",")[i]);
            newsModel.setNewsTextTitle(List.get(1).split(",")[i]);
            newsModel.setNewsText(List.get(2).split(",")[i]);
            newsModelList.add(newsModel);
        }
        return newsModelList;
    }

    /**
     * /得到home新闻
     * @return
     */
    public ArrayList<NewsModel> getNewsModelListForHome() {
        List<String> homeNewsTextTitleList = new ArrayList();
        List<String> newsTextTitleList = new ArrayList();
        NewsDao NewsDao = new NewsDao();
        int length = NewsDao.getHomeNewsTextTitlesList().size();
        for (int i = 0; i <length; i++) {
            homeNewsTextTitleList.add(NewsDao.getHomeNewsTextTitlesList().get(i));
            newsTextTitleList.add(NewsDao.getHomeNewsTextTitleDatesList().get(i));
        }
        String textTitle = Tool.replaceText(homeNewsTextTitleList.toString());
        String text = Tool.replaceText(newsTextTitleList.toString());
        String addStr = textTitle+split+text;
        //写
        IO.writeNotepad(addStr, PAHT.NEWSSERVICE_FILE_NAME.getPath());
        //读
        String[] str = IO.readNotepad().split(split);//aaaaaa@@@@bbbbbbb
        List<String> List=new ArrayList(Arrays.asList(str));//集合1{a,a,a,a,a,a,a}和集合2{b,b,b,b,b,b,b,b}
        ArrayList<NewsModel> newsModelList = new ArrayList<>();
        for (int i = 0; i <length; i++) {
            NewsModel newsModel = new NewsModel();
            newsModel.setHomeNewsTextTitle(List.get(0).split(",")[i]);
            newsModel.setHomeNewsTextTitleDate(List.get(1).split(",")[i]);
            newsModelList.add(newsModel);
        }
        return newsModelList;
    }
}
